{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 天干地支"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>ConsoleTables, 2.6.1</span></li><li><span>YiJingFramework.PrimitiveTypes, 4.0.1</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:ConsoleTables\"\n",
    "#r \"nuget:YiJingFramework.PrimitiveTypes\"\n",
    "using ConsoleTables;\n",
    "using YiJingFramework.PrimitiveTypes;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类型 `Tiangan` 和 `Dizhi` 分别表示天干和地支。\n",
    "\n",
    "总体上来说其与[五行](./五行.ipynb)类似，主要的不同之处在于其序数是具有一定意义的，因此提供了 `Index` 属性和 `FromIndex` 方法，且其值是从 `1` 开始的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| i  | t = Tiangan.FromIndex(i) | t.Index | d = Dizhi.FromIndex(i) | d.Index |\r\n",
      "|----|--------------------------|---------|------------------------|---------|\r\n",
      "| -1 | *壬 Ren                  | 9       | *戌 Xu                 | 11      |\r\n",
      "| 0  | *癸 Gui                  | 10      | *亥 Hai                | 12      |\r\n",
      "| 1  |  甲 Jia                  | 1       |  子 Zi                 | 1       |\r\n",
      "| 2  |  乙 Yi                   | 2       |  丑 Chou               | 2       |\r\n",
      "| 3  |  丙 Bing                 | 3       |  寅 Yin                | 3       |\r\n",
      "| 4  |  丁 Ding                 | 4       |  卯 Mao                | 4       |\r\n",
      "| 5  |  戊 Wu                   | 5       |  辰 Chen               | 5       |\r\n",
      "| 6  |  己 Ji                   | 6       |  巳 Si                 | 6       |\r\n",
      "| 7  |  庚 Geng                 | 7       |  午 Wu                 | 7       |\r\n",
      "| 8  |  辛 Xin                  | 8       |  未 Wei                | 8       |\r\n",
      "| 9  |  壬 Ren                  | 9       |  申 Shen               | 9       |\r\n",
      "| 10 |  癸 Gui                  | 10      |  酉 You                | 10      |\r\n",
      "| 11 | *甲 Jia                  | 1       |  戌 Xu                 | 11      |\r\n",
      "| 12 | *乙 Yi                   | 2       |  亥 Hai                | 12      |\r\n",
      "| 13 | *丙 Bing                 | 3       | *子 Zi                 | 1       |\r\n",
      "| 14 | *丁 Ding                 | 4       | *丑 Chou               | 2       |\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "var numbers = Enumerable.Range(1 - 2, 12 + 4);\n",
    "var table = new ConsoleTable(\"i\", \"t = Tiangan.FromIndex(i)\", \"t.Index\", \"d = Dizhi.FromIndex(i)\", \"d.Index\");\n",
    "foreach (var i in numbers)\n",
    "{\n",
    "    var t = Tiangan.FromIndex(i);\n",
    "    var newIT = t.Index;\n",
    "    var d = Dizhi.FromIndex(i);\n",
    "    var newID = d.Index;\n",
    "    var starT = newIT == i ? ' ' : '*';\n",
    "    var starD = newID == i ? ' ' : '*';\n",
    "    table.AddRow(i, $\"{starT}{t:C} {t}\", newIT, $\"{starD}{d:C} {d}\", newID);\n",
    "}\n",
    "table.Write(Format.Minimal);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不过，由于在使用 `default` 创建结构体时，其内部值会为零。因此，内部值不能使用上述操作的结果。考虑到这一问题，我们仍然提供了强制转换运算符，它直接使用内部值 `Index - 1` ："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| i  | t = (Tiangan)i | (int)t | d = (Dizhi)i | (int)d |\r\n",
      "|----|----------------|--------|--------------|--------|\r\n",
      "| -2 | *壬 Ren        | 8      | *戌 Xu       | 10     |\r\n",
      "| -1 | *癸 Gui        | 9      | *亥 Hai      | 11     |\r\n",
      "| 0  |  甲 Jia        | 0      |  子 Zi       | 0      |\r\n",
      "| 1  |  乙 Yi         | 1      |  丑 Chou     | 1      |\r\n",
      "| 2  |  丙 Bing       | 2      |  寅 Yin      | 2      |\r\n",
      "| 3  |  丁 Ding       | 3      |  卯 Mao      | 3      |\r\n",
      "| 4  |  戊 Wu         | 4      |  辰 Chen     | 4      |\r\n",
      "| 5  |  己 Ji         | 5      |  巳 Si       | 5      |\r\n",
      "| 6  |  庚 Geng       | 6      |  午 Wu       | 6      |\r\n",
      "| 7  |  辛 Xin        | 7      |  未 Wei      | 7      |\r\n",
      "| 8  |  壬 Ren        | 8      |  申 Shen     | 8      |\r\n",
      "| 9  |  癸 Gui        | 9      |  酉 You      | 9      |\r\n",
      "| 10 | *甲 Jia        | 0      |  戌 Xu       | 10     |\r\n",
      "| 11 | *乙 Yi         | 1      |  亥 Hai      | 11     |\r\n",
      "| 12 | *丙 Bing       | 2      | *子 Zi       | 0      |\r\n",
      "| 13 | *丁 Ding       | 3      | *丑 Chou     | 1      |\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "var numbers = Enumerable.Range(0 - 2, 12 + 4);\n",
    "var table = new ConsoleTable(\"i\", \"t = (Tiangan)i\", \"(int)t\", \"d = (Dizhi)i\", \"(int)d\");\n",
    "foreach (var i in numbers)\n",
    "{\n",
    "    var t = (Tiangan)i;\n",
    "    var newIT = (int)t;\n",
    "    var d = (Dizhi)i;\n",
    "    var newID = (int)d;\n",
    "    var starT = newIT == i ? ' ' : '*';\n",
    "    var starD = newID == i ? ' ' : '*';\n",
    "    table.AddRow(i, $\"{starT}{t:C} {t}\", newIT, $\"{starD}{d:C} {d}\", newID);\n",
    "}\n",
    "table.Write(Format.Minimal);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "python",
   "pygments_lexer": "csharp",
   "version": "3.12.2"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
